﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace CJJCH.Web.Manage
{
    public partial class NormEdit : PowerBase
    {
        CJJCH.BLL.T_INF_Norm bll_norm = new BLL.T_INF_Norm();
        CJJCH.BLL.T_INF_NormType bll_type = new BLL.T_INF_NormType();
        CJJCH.Model.T_INF_Norm model = new Model.T_INF_Norm();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Form.DefaultButton = "btnSave";
            if (!IsPostBack)
            {
                InitRequest();
                InitControl();
                ShowNorm();
            }
            LTP.Common.ButtonClass.ButtonOnClick(this.Page, btnSave, "checkform()", false);
        }

        protected void InitRequest()
        {
            if (Request.QueryString["keyword"] != null)
            {
                lblKeyword.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keyword"].Trim());
            }
            if (Request.QueryString["orderkey"] != null)
            {
                lblOrderKey.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["orderkey"].Trim());
            }
            if (Request.QueryString["ordertype"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["ordertype"].Trim()))
            {
                lblOrderType.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["ordertype"].Trim());
            }
            if (Request.QueryString["keytype"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["keytype"].Trim()))
            {
                lblKeyType.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keytype"].Trim());
            }
            if (Request.QueryString["betime"] != null && LTP.Common.CheckInput.CheckDateTime(Request.QueryString["betime"].Trim()))
            {
                lblBeTime.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["betime"].Trim());
            }
            if (Request.QueryString["edtime"] != null && LTP.Common.CheckInput.CheckDateTime(Request.QueryString["edtime"].Trim()))
            {
                lblEdTime.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["edtime"].Trim());
            }
            if (Request.QueryString["curpage"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["curpage"].ToString()))
            {
                ViewState["curpage"] = LTP.Common.StringClass.EncodeString(Request.QueryString["curpage"].ToString());
            }
            else
            {
                ViewState["curpage"] = "1";
            }
            if (!string.IsNullOrEmpty(Request.QueryString["ID"].ToString()) && LTP.Common.CheckInput.CheckGUID(Request.QueryString["ID"].ToString()))
            {
                ViewState["ID"] = Request.QueryString["ID"].ToString();
            }
            IList<CJJCH.Model.T_INF_NormType> list = new List<CJJCH.Model.T_INF_NormType>();
            list = bll_type.GetTypeList("");
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    dropType.Items.Add(new ListItem(list[i].TypeName, list[i].ID.ToString()));
                }
                list = null;
            }
        }

        protected void InitControl()
        {
            lnkQue.NavigateUrl = "NormList.aspx?keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                            + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                            + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                            + "&keytype=" + LTP.Common.StringClass.EncodeUrl(lblKeyType.Text.Trim())
                            + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblBeTime.Text.Trim())
                            + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblEdTime.Text.Trim())
                            + "&curpage=" + LTP.Common.StringClass.EncodeUrl(ViewState["curpage"].ToString());
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string error = checkInput();
            if (error == "")
            {
                bool ishot = this.chkIsHot.Checked;
                bool istop = this.chkIsTop.Checked;
                model.PlusDate = Convert.ToDateTime(this.txtPlusDate.Text.Trim());
                model.IsHot = ishot;
                model.IsTop = istop;
                model.ManagerID = SessionManagerID;
                model.NormNote = LTP.Common.StringClass.RemoveSQL(this.txtContent.Value.Trim());
                model.NormSource = LTP.Common.StringClass.EncodeString(this.txtSource.Text.Trim());
                model.NormTitle = LTP.Common.StringClass.EncodeString(this.txtTitle.Text.Trim());
                model.OrderNum = Convert.ToInt32(this.txtOrder.Text.Trim());
                model.TypeID = Convert.ToInt32(this.dropType.SelectedValue);
                model.ID = new Guid(ViewState["ID"].ToString());
                error = bll_norm.UpdateNorm(model);
                if (error == "")
                {
                    LTP.Common.WindowClass.WindowGoHref("更新成功！", "NormList.aspx?keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                        + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                        + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                        + "&keytype=" + LTP.Common.StringClass.EncodeUrl(lblKeyType.Text.Trim())
                        + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblBeTime.Text.Trim())
                        + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblEdTime.Text.Trim())
                        + "&curpage=" + LTP.Common.StringClass.EncodeUrl(ViewState["curpage"].ToString()));
                }

                else
                {
                    LTP.Common.WindowClass.WindowBack(error);
                }
            }
            else
            {
                LTP.Common.WindowClass.WindowBack(error);
            }
        }

        protected void ShowNorm()
        {
            model = bll_norm.GetNormModel(new Guid(ViewState["ID"].ToString()));
            if (model != null)
            {
                this.txtTitle.Text = model.NormTitle;
                this.txtSource.Text = model.NormSource;
                this.txtContent.Value = model.NormNote;
                this.dropType.SelectedValue = model.TypeID.ToString();
                this.chkIsHot.Checked = model.IsHot;
                this.chkIsTop.Checked = model.IsTop;
                this.txtPlusDate.Text = model.PlusDate.ToString("yyyy/MM/dd");
                this.txtOrder.Text = model.OrderNum.ToString();
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("操作错误！");
            }
        }

        protected string checkInput()
        {
            StringBuilder error = new StringBuilder();
            if (this.txtTitle.Text.Trim() == "")
            {
                error.Append("请输入标题！ \\n");
            }
            if (this.dropType.SelectedValue == "")
            {
                error.Append("请选择规范解释类别！ \\n");
            }
            else if (!LTP.Common.CheckInput.CheckDigit(this.dropType.SelectedValue))
            {
                error.Append("请选择正确的规范解释类别！ \\n");
            }
            if (this.txtPlusDate.Text == "")
            {
                error.Append("请选择发布日期！ \\n");
            }
            else if (!LTP.Common.CheckInput.CheckDateTime(this.txtPlusDate.Text.Trim()))
            {
                error.Append("请选择正确的发布日期！ \\n");
            }
            if (this.txtContent.Value == "")
            {
                error.Append("请输入内容！ \\n");
            }
            if (this.txtOrder.Text.Trim() == "")
            {
                error.Append("请输入排序号！ \\n");
            }
            else if (!LTP.Common.CheckInput.CheckDigit(this.txtOrder.Text.Trim()))
            {
                error.Append("请输入正确的排序号！ \\n");
            }
            return error.ToString();
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("NormList.aspx?keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                            + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                            + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                            + "&keytype=" + LTP.Common.StringClass.EncodeUrl(lblKeyType.Text.Trim())
                            + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblBeTime.Text.Trim())
                            + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblEdTime.Text.Trim())
                            + "&curpage=" + LTP.Common.StringClass.EncodeUrl(ViewState["curpage"].ToString()));
        }
    }
}